package club

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"shimmer/neusoft_club_backend/controllers/cons"
	"shimmer/neusoft_club_backend/controllers/user_controller/club/application"
	"shimmer/neusoft_club_backend/controllers/user_controller/club/owned"
	"shimmer/neusoft_club_backend/controllers/user_controller/club/position"
	"shimmer/neusoft_club_backend/controllers/utils"
	"shimmer/neusoft_club_backend/dao/domain"
	"shimmer/neusoft_club_backend/services"
	"shimmer/neusoft_club_backend/services/club_service"
)

// @Summary 获取用户所加入的社团
// @Description
// @Tags 用户
// @accept json
// @Produce json
// @Param page query integer true "页数"
// @Param size query integer true "页面大小"
// @Success 200 {object} []domain.ClubDTO
// @Router /api/user/self/club [get]
func getClubsWithUserJoined(c *gin.Context) {
	user,err := domain.GetUserFromContext(c)
	if err != nil {
		services.AbortWithError(c,err)
		return
	}
	pageQuery,err := utils.GetPaginationFromContext(c)
	if err != nil {
		services.AbortWithError(c,err)
		return
	}
	res,err := club_service.GetClubsWithUserJoined(user.ID,pageQuery)
	if err != nil {
		services.AbortWithDatabaseError(c,err)
		return
	}
	c.JSON(http.StatusOK,res)
}
// @Summary 退出社团
// @Description
// @Tags 用户
// @accept json
// @Produce json
// @Param clubID path integer true "社团ID"
// @Success 204
// @Router /api/user/self/club/single/{clubID} [delete]
func exitClub(c *gin.Context) {
	user,err := domain.GetUserFromContext(c)
	if err != nil {
		services.AbortWithError(c,err)
		return
	}
	clubQuery := &cons.ClubUriParamBinding{}
	if err = c.ShouldBindUri(clubQuery);err != nil {
		services.AbortWithError(c,err)
		return
	}
	err = club_service.ExitClub(user.ID, clubQuery.ClubID)
	if err != nil {
		services.AbortWithError(c,err)
		return
	}
	c.AbortWithStatus(http.StatusNoContent)
}
func ConfigureUserClubController(r *gin.RouterGroup) {

	r.GET("/", getClubsWithUserJoined)
	r.DELETE(fmt.Sprintf("/single/:%s",cons.ClubUriParam), exitClub)
	{
		applicationGroup := r.Group(fmt.Sprintf("/single/:%s/application",cons.ClubUriParam))
		application.ConfigureUserApplicationContainer(applicationGroup)
	}
	{
		ownedGroup := r.Group(fmt.Sprintf("/single/:%s/owned",cons.ClubUriParam))
		owned.ConfigureUserClubOwnedController(ownedGroup)
	}
	{
		positionGroup := r.Group(fmt.Sprintf("/single/:%s/position",cons.ClubUriParam))
		position.ConfigureUserPositionController(positionGroup)
	}

}